import sys
sys.path.append("G:\pycharm-workspace\precisiongene\crcmodel")
import tensorflow as tf
import pickle
import numpy as np
from sklearn.metrics import accuracy_score

from src.config import config, params
from src.datas import OtuData

hps = params.get_default_params()

tf.reset_default_graph()
init = tf.global_variables_initializer()

X_test, y_test = pickle.load(open(config.test_pkl, 'rb'))
batch_size = X_test.shape[0] if X_test.shape[0] < hps.batch_size else hps.batch_size

test_dataset = OtuData.OtuData(
    X_test, y_test)

y_predict = []
test_labels_list = []
with tf.Session() as sess:
    sess.run(init)
    saver = tf.train.import_meta_graph(config.meta_path)
    saver.restore(sess, tf.train.latest_checkpoint(config.latest_model_path))

    y_pred = sess.graph.get_tensor_by_name("metrics/y_pred_model:0")

    for i in range(round(X_test.shape[0] / hps.batch_size)):
        test_inputs_o, test_labels_o = test_dataset.next_batch(batch_size)

        test_inputs = test_inputs_o
        test_labels = test_labels_o
        if test_inputs.shape[0] < hps.batch_size:
            last_input = test_inputs[-1]
            last_label = test_labels[-1]
            for _ in range(hps.batch_size - test_inputs.shape[0]):
                test_inputs = np.insert(test_inputs, -1, values=last_input, axis=0)
                test_labels = np.insert(test_labels, -1, values=last_label, axis=0)

        y_pred_val = sess.run([y_pred], feed_dict={"inputs:0": test_inputs,
                                                   "keep_prob:0": params.test_keep_prob_value, })
        print("test_labels_o:{}\ny_pred_val:{}".format(test_labels[:test_labels_o.shape[0]],
                                                       y_pred_val[:test_labels_o.shape[0]]))
        test_labels_list += test_labels[:test_labels_o.shape[0]].tolist()
        y_predict += y_pred_val[0].tolist()[:test_labels_o.shape[0]]

print("accuracy：{}".format(accuracy_score(test_labels_list, y_predict)))  # 0.864
